<?php

/**
 * session操作类
 * 
 * @author keluo<pycorvn@yeah.net>
 * @since 2015-01-24
 */
class SCSession {
    
    /**
     * session标识
     * @var string
     */
    private $_sessionID;
    
    private $_data;
    
    public function __construct() {
        if(!isset($_SESSION)) {
            session_start();
        }
        $this->_sessionID = session_id();
        if(isset($_SESSION[$this->_sessionID])) {
            $this->_data = unserialize($_SESSION[$this->_sessionID]);
        } else {
            $this->_data = array();
        }
    }
    
    public function __set($key, $value) {
        $this->_data[$key] = $value;
        $_SESSION[$this->_sessionID] = serialize($this->_data);
    }

    public function __get($key) {
        return isset($this->_data[$key]) ? $this->_data[$key] : false;
    }
    
    public function delete($key) {
        if(isset($this->_data[$key])) {
            unset($this->_data[$key]);
            $_SESSION[$this->_sessionID] = serialize($this->_data);
        }
    }
    
    public function clear() {
        $this->_data = array();
        session_unset();
    }
}